﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Sobiens.Connector.Admin.DAL;
using System.Xml.Linq;
using System.Data;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using Sobiens.Connector.Admin.BLL;
using System.Runtime.Serialization.Json;

namespace Sobiens.Connector.Admin.UI
{
    class ConnectorAdminModelAssembler
    {
        private ConfigurationList _list = new ConfigurationList();

        private ConnectorAdminModel _model = new ConnectorAdminModel();

        public ConnectorAdminModel GetModelForConfiguration(String configurationId)
        {
            PopulateModel();
            _model.SelectedConfiguration = GetConfiguration(configurationId);

            return _model;
        }

        private void PopulateModel()
        {
            _model.AllConfigurations = new Dictionary<String, String> { { Guid.Empty.ToString(), "Please select..." } };

            AllConfigurations resultList = new AllConfigurations();
            
            SPListItemCollection itemsConfigurationsTitleAndDescription = _list.GetAllItems(new string[] { ConfigurationList.FIELDS.TITLE, ConfigurationList.FIELDS.DESCRIPTION });
            foreach (SPListItem item in itemsConfigurationsTitleAndDescription)
            {
                _model.AllConfigurations.Add(item.UniqueId.ToString(), item[ConfigurationList.FIELDS.TITLE].ToString());

                AppConfiguration configuration = new AppConfiguration();
                configuration.Title = item[ConfigurationList.FIELDS.TITLE].ToString();
                configuration.UniqueID = item.UniqueId.ToString();
                configuration.Description = item[ConfigurationList.FIELDS.DESCRIPTION].ToString();
                configuration.ID = item.ID;

                resultList.Add(configuration);
            }

            _model.AllConfigurationsJSON = resultList.GetJSON();
        }

        
        private Configuration GetConfiguration(String configurationId)
        {
            Configuration config = new Configuration();

            if (String.IsNullOrEmpty(configurationId) || (configurationId == Guid.Empty.ToString()))
                return config;
            
            SPListItem itemSelectedConfiguration = _list.GetItem(new Guid(configurationId));
            config.UniqueId = itemSelectedConfiguration.UniqueId;
            config.Title = itemSelectedConfiguration.Title;
            config.Description = itemSelectedConfiguration[ConfigurationList.FIELDS.DESCRIPTION].ToString();

            String xmlData = ((itemSelectedConfiguration != null) && (itemSelectedConfiguration[ConfigurationList.FIELDS.DATA] != null)) ? itemSelectedConfiguration[ConfigurationList.FIELDS.DATA].ToString().Trim() : String.Empty;
            Sobiens.Connectors.Entities.Settings.AppConfiguration data = Sobiens.Connectors.Entities.Settings.AppConfiguration.CreateFromXML(xmlData);
            config.JSONData = data.GetJSON().Replace(@"\\", @"\\\\"); //TODO: add additional escaping if necessary

            return config;
        }

    }
}
